﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors.Repository;

using DevTeamIT;
using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Araclar
{
    public partial class UserChanger : XPopupForm
    {
        public UserChanger()
        {
            InitializeComponent();
            /*
            
            //riCikisTipi.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            
            */

            RepositoryItemGridLookUpEdit riCikisTipi = GetCustomGLE(RepositoryManager.dsi.V_User, "Username", "UserId");
            gleUser.Properties.Assign(riCikisTipi);

            //gleUser.Properties.Assign(RI(Rp.V_User));
            gleUser.EditValue = WhoAmI;
        }

        private void UserChanger_Load(object sender, EventArgs e)
        {
            gleUser.Closed += gleUser_Closed;
        }

        void gleUser_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            int userId = (int)gleUser.EditValue;
            ITaskManager.Datasets.MyData.V_UserRow newUser = RepositoryManager.dsi.V_User.FindByUserId(userId);

            Session.CurrentUser.UserId = newUser.UserId;
            Session.CurrentUser.DepartmentId = newUser.DepartmentId;
            if (!newUser.IsEmailNull())
                Session.CurrentUser.Email = newUser.Email;
            Session.CurrentUser.Name = newUser.Name;
            Session.CurrentUser.UserName = newUser.Username;
            Session.CurrentUser.UserId = newUser.UserId;
            AppSession.MainForm.bsiUser.Caption = Session.CurrentUser.UserName + " / " + Session.DatabaseName;
            //Notify("Yeni Kullanıcı: " + newUser.Name);
            this.DialogResult = DialogResult.OK;
        }
    }
}
